package private

import (
	"dc-go/global"
	"dc-go/model"
	"errors"
	"time"
)

type ServiceOrgRole struct{}

func (s *ServiceOrgRole) GetOrgRoleList(orgCode *model.RequestOrgCode) (orgRole []model.SysOrgRole, err error) {
	err = global.DcDb.Where("org_code=?", orgCode.OrgCode).Find(&orgRole).Error
	return orgRole, err
}

func (s *ServiceOrgRole) EditOrgRole(toEditOrgRole *model.RequestOrgRoleEdit, empeeAcct string) (err error) {
	// var role model.SysRole
	// err = global.DcDb.Where("role_code = ?", toEditRoleMenu.RoleCode).First(&role).Error
	// if err != nil {
	// 	return err
	// }
	var orgRolesBefore []model.SysOrgRole
	err = global.DcDb.Where("org_code = ?", toEditOrgRole.OrgCode).Find(&orgRolesBefore).Error
	for _, roleCode := range toEditOrgRole.RoleCode {
		//如果是新增的菜单，则插入数据
		var isExist bool
		for _, orgRoleBefore := range orgRolesBefore {
			if orgRoleBefore.RoleCode == roleCode {
				isExist = true
				break
			}
		}
		if !isExist {
			var orgRole model.SysOrgRole
			orgRole.OrgCode = toEditOrgRole.OrgCode
			orgRole.RoleCode = roleCode
			orgRole.CreatedBy = empeeAcct
			orgRole.CreatedAt = time.Now()
			orgRole.UpdatedBy = empeeAcct
			orgRole.UpdatedAt = time.Now()
			orgRole.OrgRoleCode = toEditOrgRole.OrgCode + "-" + roleCode
			err = global.DcDb.Create(&orgRole).Error
			if err != nil {
				return err
			}
		}
	}
	//删除旧的菜单
	for _, orgRoleBefore := range orgRolesBefore {
		var isExist bool
		for _, roleCode := range toEditOrgRole.RoleCode {
			if orgRoleBefore.RoleCode == roleCode {
				isExist = true
				break
			}
		}
		if !isExist {
			err = global.DcDb.Where("org_code = ? and role_code = ?", toEditOrgRole.OrgCode, orgRoleBefore.RoleCode).Delete(&model.SysOrgRole{}).Error
			if err != nil {
				return err
			}
		}
	}
	return err
}

func (s *ServiceOrgRole) QueryUserByOrgRole(orgRoleCode *model.RequestOrgRoleCode) (user []model.SysUser, err error) {
	orgRole := model.SysOrgRole{
		OrgRoleCode: orgRoleCode.OrgRoleCode,
	}
	var userList []model.SysUser
	err = global.DcDb.Model(&orgRole).Association("User").Find(&userList)
	return userList, err
}

func (s *ServiceOrgRole) RemoveUserOrgRole(toRemoveUserOrgRole *model.RequestUserOrgRole) (err error) {
	var userOrgRole model.SysUserOrgRole
	if toRemoveUserOrgRole.EmpeeAcct == "admin" {
		return errors.New("抱歉, 你不能修改超级管理员! ")
	}
	err = global.DcDb.Where("org_role_code = ? and empee_acct = ?", toRemoveUserOrgRole.OrgRoleCode, toRemoveUserOrgRole.EmpeeAcct).Delete(&userOrgRole).Error
	return err
}

func (s *ServiceOrgRole) AddUserOrgRole(toAddUserOrgRole *model.RequestUserOrgRoleAdd) (err error) {
	var userOrgRole []model.RequestUserOrgRole
	for _, r := range toAddUserOrgRole.OrgRoleCode {
		ur := model.RequestUserOrgRole{
			OrgRoleCode: r,
			EmpeeAcct:   toAddUserOrgRole.EmpeeAcct,
		}
		userOrgRole = append(userOrgRole, ur)
	}
	if len(userOrgRole) != 0 {
		err = global.DcDb.Model(&model.SysUserOrgRole{}).Save(&userOrgRole).Error
		return err
	} else {
		return errors.New("本次操作没有影响! ")
	}
}
